package com.pic.mgt;

import com.pic.mgt.action.ScheduleAutoInspectMissUnitAction;
import com.pic.mgt.action.ShutdownSystemAction;
import com.pic.mgt.action.UnitMonitorControlAction;
import com.pic.mgt.cli.CommandLineArgParser;
import com.pic.mgt.config.ConfigBean;
import com.pic.mgt.config.ConfigurationException;
import com.pic.mgt.haishin.HaishinConfigBean;
import com.pic.mgt.haishin.HaishinScheduler;
import com.pic.mgt.haishin.HaishinSchedulerImpl;
import com.pic.mgt.scheduler.SchedulerException;
import com.pic.mgt.utils.RunTimeSystemUtils;

public class HaishinLauncher extends Launcher {
	private static HaishinLauncher haishinLauncher = null;
	
	private HaishinLauncher(CommandLineArgParser igalaxyCLI) throws SystemLaunchException {
		super(igalaxyCLI); 
		
		/*
		 *  (1) 若參數列有帶 'start' 參數，則自動執行開始動作。
		 *  (此 start 的參數，表示開始 Unit 監控)
		 */
		if (igalaxyCLI.hasArg("start")) {
			startHaishinMonitor();
		}
		
		// (2) 若參數列有帶 'recover'，
		if (igalaxyCLI.hasArg("recover")) {
			doAutoMissUnitInspectionAndRecoveryWithSystemDelay();
		}
		
		// (3) 開始 Scheduler 排程
		try {
			HaishinScheduler haishinScheduler = super.applicationContext.getBean(
																"haishinSchedulerImpl",
																HaishinSchedulerImpl.class);
			haishinScheduler.startScheduler();
		} catch (SchedulerException e1) {
			e1.printStackTrace();
		}
		
		
		// -================== 以下為測試用的 code
		
		/*RunTimeSystemUtils.threadSleep(10000);

		ShutdownSystemAction shutdownSystemAction =
			Launcher.applicationContext.getBean("shutdownSystemActionImpl",
					ShutdownSystemAction.class);
		shutdownSystemAction.shutdownSystem();*/
		
		/*HaishinScheduler haishinScheduler = super.applicationContext.getBean("haishinSchedulerImpl", HaishinScheduler.class);
		
		try {
			haishinScheduler.getScheduleJobInfoByGroupName(groupName);
		} catch (ScheduleAutoJobException e) {
			e.printStackTrace();
		}*/
		
		/*Calendar cal = Calendar.getInstance();
		cal.add(Calendar.HOUR_OF_DAY, 5);
		try {
			autoJob.scheduleSimpleJob(cal.getTime(),
									  TestJob.class,
									  Collections.EMPTY_MAP,
									  "Test",
									  "TestGroup");
		} catch (ScheduleAutoJobException e) {
			Throwable t = e.getCause();
			if (ObjectAlreadyExistsException.class.isInstance(t)) {
				System.out.println("重覆的排程!!!");
			}
			else {
				e.printStackTrace();
			}
		}*/
		
		
		// --- 測試  Message Miss Inspection 功能
		/*HaishinMissMessageUnitInspectActionImpl haishinMissMessageUnitInspectActionImpl = 
					super.applicationContext.getBean("haishinMissMessageUnitInspectActionImpl", HaishinMissMessageUnitInspectActionImpl.class);
		haishinMissMessageUnitInspectActionImpl.inspectMissUnitAndRecover();*/
		
		
		// --- 測試 resend 功能
		/*RunTimeSystemUtils.threadSleep(3000);
		HaishinBusinessResendActionImpl haishinBusinessResendActionImpl = super.applicationContext.getBean(
				"haishinBusinessResendActionImpl",
				HaishinBusinessResendActionImpl.class);
		haishinBusinessResendActionImpl.resendBusinessToTerminal("PGMZ_05200001", "0050050501");*/
		
		// --- 測試 business recover 功能
		/*RunTimeSystemUtils.threadSleep(3000);
		HaishinBusinessRecoverActionImpl haishinBusinessRecoverActionImpl = super.applicationContext.getBean(
				"haishinBusinessRecoverActionImpl",
				HaishinBusinessRecoverActionImpl.class);
		haishinBusinessRecoverActionImpl.recoverBusiness("MSTD_05200001");*/
		
		// --- 測試 message recover 功能
		/*RunTimeSystemUtils.threadSleep(3000);
		HaishinMessageRecoverActionImpl haishinMessageRecoverActionImpl =
			super.applicationContext.getBean("haishinMessageRecoverActionImpl",
											 HaishinMessageRecoverActionImpl.class);
		haishinMessageRecoverActionImpl.recoverMessage("0050050201_MSTD_11130001");*/
	}

	@Override
	protected String[] applicationContextResources() {
		 String[] serviceResources =
		 		{"application-context-haishin.xml",
				 "haishin-resource.xml",
				 "haishin-config.xml",
				 "haishin-action.xml",
				 "haishin-action-shell.xml",
				 "haishin-parser.xml",
				 "haishin-executer.xml",
				 "haishin-executer-asgmt.xml",
				 "haishin-executer-target.xml",
				 "haishin-quartz.xml",
				 "haishin-dao.xml",
				 "haishin-console.xml",
				 "common-parser.xml",
				 "common-system.xml",
				 "common-config.xml",
				 "common-dao.xml",
				 "common-console.xml",
				 "common-action.xml",
				 "common-action-shell.xml",
		 		 "common-scheduler.xml"};
		 return serviceResources;
	}
	
	@Override
	protected void checkSpecificConfiguration() throws ConfigurationException {
		ConfigBean haishinConfigBean =
			super.applicationContext.getBean("haishinConfig", 
											 HaishinConfigBean.class);
		// TODO 檢查  HaishinConfigBean 格式
	}
	
	/**
	 * 載入系統並回傳 HaishinLauncher。
	 * @return
	 * @throws SystemLaunchException 
	 */
	public synchronized static HaishinLauncher getHaishinLauncher(CommandLineArgParser igalaxyCLI)
										throws SystemLaunchException {
		if (haishinLauncher == null) {
			haishinLauncher = new HaishinLauncher(igalaxyCLI);
		}
		return haishinLauncher;
	}
	
	/**
	 * 開始配信作業,包含開始 (1)監控業務 (2)監控訊息
	 */
	private void startHaishinMonitor() {
		UnitMonitorControlAction unitMonitorControlAction = 
			super.applicationContext.getBean("unitMonitorControlActionImpl",
											 UnitMonitorControlAction.class);
		
		unitMonitorControlAction.startUnitMonitor();
	}
	
	/**
	 * 將 "自動檢測lose業務和訊息並自動做恢復" 的處理動作加入排程, delay 一段系統
	 * 時間(由系統自行判斷)後才開始做。
	 */
	private void doAutoMissUnitInspectionAndRecoveryWithSystemDelay() {
		ScheduleAutoInspectMissUnitAction scheduleAutoInspectMissUnitAction =
						super.applicationContext.getBean("scheduleAutoInspectMissUnitActionImpl",
														 ScheduleAutoInspectMissUnitAction.class);
		scheduleAutoInspectMissUnitAction.scheduleAutoInspectMissUnitAndRecover();
	}
}
